package com.labManage.controller;

import com.labManage.dao.RoleDao;
import com.labManage.entity.Role;
import com.labManage.entity.User;
import com.labManage.service.RoleService;
import com.labManage.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleDao roleDao;

    @RequestMapping("getAllRole")
    @ResponseBody
    public ResultVo getAllRole(){
        try{
            List<Role> roleList = roleService.getAllRole();
            return ResultVo.build(roleList.size(),0,"",roleList);
        }catch (Exception e){
            return ResultVo.build(500,"服务器异常");
        }
    }

    @RequestMapping("/getAllRoleLimit")
    @ResponseBody
    public ResultVo getAllRoleLimit(Integer page,Integer limit,Role condition){
        try{
            List<Role> roleList = roleService.getAllRoleLimit(page,limit,condition);
            return ResultVo.build(roleList.size(),0,"",roleList);
        }catch (Exception e){
            return ResultVo.build(500,"服务器异常");
        }
    }

    @RequestMapping("/modifyRoleInfoById")
    @ResponseBody
    @RequiresPermissions("role:modify")
    public ResultVo modifyRoleInfoById(Role role,String permitionIds){
        try{
            List<Long> permitionIdsList = null;
            if(permitionIds != null && !permitionIds.trim().equals("")){
                String[] array = permitionIds.split(",");
                permitionIdsList = new ArrayList<>();
                for (int i = 0; i < array.length; i++) {
                    permitionIdsList.add(Long.parseLong(array[i]));
                }
            }
            if(null==permitionIdsList){
                return ResultVo.build(201,"请至少分配一个权限");
            }
            int result = roleService.modifyRoleInfoById(role,permitionIdsList);
            if(result>=0){
                return ResultVo.build(0,"修改成功");
            }else {
                return ResultVo.build(201,"修改失败");
            }
        }catch (Exception e){
            return ResultVo.build(500,"服务器异常");
        }
    }

    @RequestMapping("/getAllRoleByUserId")
    @ResponseBody
    public ResultVo getAllRoleByUserId(Long userId){
        if(null!=userId){
            try{
                List<Role> roles = roleService.getAllRoleByUserId(userId);
                if(roles!=null){
                    return ResultVo.build(0,"",roles);
                }else {
                    return ResultVo.build(201,"修改失败");
                }
            }catch (Exception e){
                return ResultVo.build(500,"服务器异常");
            }
        }else{
            return ResultVo.build(500,"服务器异常");
        }
    }

    @RequestMapping("/getAllRoleByLoginUserId")
    @ResponseBody
    public ResultVo getAllRoleByLoginUserId(){
        Subject subject = SecurityUtils.getSubject();
        User loginUser = (User)subject.getPrincipal();
        try{
            List<Role> roles = roleService.getAllRoleByUserId(loginUser.getId());
            if(roles!=null){
                return ResultVo.build(0,"",roles);
            }else {
                return ResultVo.build(201,"");
            }
        }catch (Exception e){
            return ResultVo.build(500,"服务器异常");
        }
    }

    @RequestMapping("/addRoleInfo")
    @ResponseBody
    @RequiresPermissions("/role:add")
    public ResultVo addRoleInfo(Role role,String permitionIds){
        List<Long> permitionIdsList = null;
        if(permitionIds != null && !permitionIds.trim().equals("")){
            String[] array = permitionIds.split(",");
            permitionIdsList = new ArrayList<>();
            for (int i = 0; i < array.length; i++) {
                permitionIdsList.add(Long.parseLong(array[i]));
            }
        }
        if(null==permitionIdsList){
            return ResultVo.build(201,"请至少分配一个权限");
        }
        int result = roleService.addRoleInfo(role,permitionIdsList);
        if(result>=0){
            return ResultVo.build(0,"添加成功");
        }else{
            return ResultVo.build(201,"添加失败");
        }
    }

    @RequestMapping("/deleteRoleInfo")
    @ResponseBody
    @RequiresPermissions("role:delete")
    public ResultVo deleteRoleInfo(Long roleId){
        if(null!=roleId){
            int result = roleService.deleteRoleInfo(roleId);
            if(result>=0){
                return ResultVo.build(0,"删除成功");
            }else{
                return ResultVo.build(201,"删除失败");
            }
        }else{
            return ResultVo.build(201,"删除失败");
        }
    }

    @RequestMapping("/getRoleNum")
    @ResponseBody
    public ResultVo getRoleNum(){
        long roleNum = roleDao.selectRoleNum();
        return ResultVo.build(0,"",roleNum);
    }
}
